package com.example.shardingjdbcdemo.controller;

import com.example.shardingjdbcdemo.pojo.ScsProduct;
import com.example.shardingjdbcdemo.service.ShardingService;
import groovy.util.logging.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import javax.annotation.Resource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@RestController
@RequestMapping("/shardingJdbcController")
@Slf4j
public class ShardingJdbcController {
    @Resource
    private ShardingService shardingService;

    @PostMapping("/insertProduct")
    @CrossOrigin
    public String insertProduct(){
        ScsProduct scsProduct = new ScsProduct();
        for(int i = 1 ; i < 10 ; i++){
            scsProduct.setProductAmount(i);
            scsProduct.setProductCode("SHARDING-JDBC"+i);
            shardingService.insertProduct(scsProduct);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return "插入成功！";
    }
    @PostMapping("/queryProduct")
    @CrossOrigin
    public List<ScsProduct> queryProduct(){
        return shardingService.queryProduct();
    }

    @PostMapping("/queryProductPage")
    @CrossOrigin
    public PageInfo<ScsProduct> queryProductPage(){
        return shardingService.queryProductPage();
    }
}
